package com.wngbms.async;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

@Slf4j
public class ForkJoinTest {

    public static void main(String[] args) {
        ForkJoinPool fjp = new ForkJoinPool(4);
        Fib fib = new Fib(100);

        Integer invoke = fjp.invoke(fib);
        log.info("invoke:{}", invoke);
    }

    public static class Fib extends RecursiveTask<Integer> {
        final int n;

        public Fib(int n) {
            this.n = n;
        }

        @Override
        protected Integer compute() {
            if (n <= 2) {
                return n;
            }
//            log.info("n:{}", n);

            Fib f1 = new Fib(n - 1);
            Fib f2 = new Fib(n - 2);
            f1.fork();
            return f2.compute() + f1.join();
        }
    }

}
